<template>
  <d2-container>
    <div class="row-ac pb-2">
      <div><el-button type="warning" @click="addCountry" icon="el-icon-plus">添加产地</el-button></div>
      <div class="ml-2"><el-button type="warning" @click="addBrand" icon="el-icon-plus">添加品牌</el-button></div>
    </div>
    <div class="d-f">
      <div class="country-box">
        <div class="country-title row text-white">产地</div>
        <div class="flex-1 mt-2 mx-1">
          <el-table :data="countryList" stripe>
            <el-table-column label="国家" prop="country_name" width="90" />
            <el-table-column align="right" label="操作">
              <template slot-scope="scope">
                <i class="el-icon-edit mr-2 cp" @click="handleCountryEdit(scope.row.id)"></i>
                <i class="el-icon-delete cp" @click="openCountryDelete(scope.row.id)"></i>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div class="flex-1 ml-2">
        <el-table :data="brandsList" border stripe>
          <el-table-column align="center" type="selection" width="50" />
          <el-table-column align="center" label="序号" type="index" width="50" />
          <el-table-column align="center" label="ID" prop="id" width="45" />
          <el-table-column align="center" label="品牌名称" prop="brand_name" width="130" />
          <el-table-column align="center" label="产地" prop="country_name" width="130" />
          <el-table-column align="center" label="排序" prop="sort" width="45" />
          <el-table-column align="center" label="添加时间" prop="create_time" width="130" />
          <el-table-column align="center" label="操作" width="180">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
              <el-button size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog :visible.sync="dialogVisible" width="30%" title="产地">
      <el-form :model="formCountry" :rules="rules" ref="form" label-width="80px">
        <el-form-item label="名称" prop="country_name">
          <div style="width: 50%;">
            <el-input v-model="formCountry.country_name"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <div style="width: 18%;">
            <el-input v-model="formCountry.sort"></el-input>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
  import {
    brandsListUrl,
    brandsdetailUrl,
    brandsCreateUrl,
    brandsUpdateUrl,
    brandsDeleteUrl,
    countryListUrl,
    countrydetailUrl,
    countryCreateUrl,
    countryUpdateUrl,
    countryDeleteUrl,
  } from "@/utils/product"
  export default {
    name: 'brand',
    data() {
      return {
        dialogVisible: false,
        queryInfo: {
          page: 1,
          pageSize: 10
        },
        brandsList: [],
        countryList: [],
        formCountry: {
          country_name: '',
          sort: 1
        },
        rules: {
          country_name: [{
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }]
        },
        addCountryStatu: true,
        getCountryId:''
      }
    },
    created() {
      this.initData()
    },
    methods: {
      initData() {
        this.getBrandsList()
        this.getCountryList()
      },
      getBrandsList() {
        this.$get(brandsListUrl, this.queryInfo).then(res => {
          this.brandsList = res.data.data.list
          this.total = res.data.data.total
        })
      },
      getCountryList() {
        this.$get(countryListUrl, this.queryInfo).then(res => {
          this.countryList = res.data.data.list
          this.total = res.data.data.total
        })
      },
      // 提交表单
      submitForm() {
        if (this.addCountryStatu) {
          this.$post(countryCreateUrl, this.formCountry).then(res => {
              this.getCountryList()
              this.dialogVisible = false
              this.$message.success('添加成功')
              this.formCountry = {}
              // 刷新分类列表
              this.$emit('refresh')
            })
            .catch(error => {
              console.log(error)
              this.$message.error('添加失败')
            })
        } else {
          this.$put(countryUpdateUrl + this.getCountryId + '/', this.formCountry).then(res => {
              this.getCountryList()
              this.dialogVisible = false
              this.$message.success('修改成功')
              this.formCountry = {}
              // 刷新分类列表
              this.$emit('refresh')
            })
            .catch(error => {
              console.log(error)
              this.$message.error('添加失败')
            })
        }
      },
      addCountry() {
        this.dialogVisible = true
      },
      addBrand() {
        this.$router.push('/addBrand');
      },
      handleEdit(brandId) {
        this.$router.push({
          path: '/addBrand',
          query: {
            brandId: brandId
          }
        });
      },
      openDelete(brandId) {
        this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 在确定时执行handleDelete的删除
          this.$del(brandsDeleteUrl + brandId + '/').then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getBrandsList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleCountryEdit(countryId) {
        this.addCountryStatu = false
        this.dialogVisible = true
        this.getCountryId = countryId
        this.$get(countrydetailUrl + countryId + '/').then(res => {
          if (res.data.code == 200) {
            this.formCountry = {
              country_name: res.data.data.country_name,
              sort: res.data.data.sort
            }
          }
        })
      },
      openCountryDelete(countryId){
        this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 在确定时执行handleDelete的删除
          this.$del(countryDeleteUrl + countryId + '/').then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getCountryList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>
<style lang="scss">
  .country-box {
    width: 16%;

    .country-title {
      height: 38px;
      min-height: 38px;
      background-color: #62A8D1;
    }

    .country-list {
      .iconfont {
        color: rgb(198, 105, 159);
      }

      .country-name:hover {
        color: rgb(255, 153, 0);
      }
    }
  }
</style>
